package com.niit.galsearch.controller;

import com.niit.entity.Game;
import com.niit.entity.User;
import com.niit.galsearch.service.SearchService;
import com.niit.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
    @RequestMapping("/search")
    public class SearchController {
        @Resource
        private SearchService searchService;



    @GetMapping("/sample")
    public CommonResult<List<Game>> getSampleObject() {
        List<Game> sampleObject = searchService.sample();
        return new  CommonResult<List<Game>> (200,"搜索成功", sampleObject);
    }
    @PostMapping("/search")
    public CommonResult<List<Game>> search (HttpSession session, @RequestBody Game game1) {
        List<Game> games = searchService.search(game1);
        return new CommonResult<List<Game>>(200,"搜索成功", games);
    }
    @GetMapping("/searchByName")
    public CommonResult<List<Game>> searchGamesByName(@RequestParam(value = "name", required = false) String gameName) {
    if (gameName != null) {
        List<Game> gamesByName = searchService.searchGamesByName(gameName);
        if (!gamesByName.isEmpty()) {
            return new CommonResult<List<Game>>(200, "搜索成功", gamesByName);
        } else {
            return new CommonResult<List<Game>>(404, "未找到匹配游戏");
        }
    } else {
        return new CommonResult<List<Game>>(400, "游戏不存在");
    }
}

    @GetMapping("/searchById")
    public CommonResult<Game> searchGamesById(@RequestParam(value = "gid", required = false) Integer gameId) {
        if (gameId != null) {
           Game gamesById = searchService.searchGamesById(gameId);
            if (gamesById!=null) {
                return new CommonResult<Game>(200, "搜索成功", gamesById);
            } else {
                return new CommonResult(404, "未找到匹配游戏");
            }
        } else {
            return new CommonResult(400, "游戏不存在");
        }
    }


    @GetMapping("/searchByLabel")
    public CommonResult<List<Game>> searchGamesByLabel(@RequestParam(value = "label") String label) {
        List<Game> gamesByLabel = searchService.searchGamesByLabel(label);
        if (!gamesByLabel.isEmpty()) {
            return new CommonResult<List<Game>>(200, "按照标签搜索成功", gamesByLabel);
        } else {
            return new CommonResult<List<Game>>(404, "未找到匹配游戏");
        }
    }
    @GetMapping("/searchByPublisher")
    public CommonResult<List<Game>> searchGamesByPublisher(@RequestParam(value = "publisher") String publisher ) {
        List<Game> gamesByPublisher = searchService.searchGamesByPublisher(publisher);
        if (!gamesByPublisher.isEmpty()) {
            return new CommonResult<List<Game>>(200, "按照发行商搜索成功", gamesByPublisher);
        } else {
            return new CommonResult<List<Game>>(404, "未找到匹配游戏");
        }
    }

    }

